package com.erp.myAdapter

import android.annotation.SuppressLint
import android.content.Context
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.LinearLayout
import com.erp.bean.ImgBean
import com.erp.R
import com.erp.util.Myutil
import com.erp.util.Sputil
import kotlinx.android.synthetic.main.lv_iv_item.view.*

/**
 * Created by Administrator on 2016/11/17/017.
 * 作者：刘洋
 */
class GaugeivGvAdapter1() : BaseAdapter() {

    lateinit var minflater: LayoutInflater
    internal var width = 0
    lateinit var mlistimg : List<ImgBean>
    lateinit var mcontext : Context
    var isadd = false

    constructor(mlistimg: List<ImgBean>,  mcontext: Context) : this( ) {
        this.mcontext=mcontext
        this.mlistimg=mlistimg
        width = Sputil.sp("erp").getInt("width", width)
        this.minflater = LayoutInflater.from(mcontext)
    }
    constructor(mlistimg: List<ImgBean>,  mcontext: Context ,b: Boolean) : this( ) {
        this.mcontext=mcontext
        this.mlistimg=mlistimg
        this.isadd=b
        width = Sputil.sp("erp").getInt("width", width)
        this.minflater = LayoutInflater.from(mcontext)
    }
    override fun getCount(): Int = if(isadd){
        mlistimg.size
    }else {
        mlistimg.size + 1
    }

    override fun getItem(i: Int): Any = mlistimg[i]

    override fun getItemId(i: Int): Long = i.toLong()

    @SuppressLint("ViewHolder")
    override fun getView(i: Int, view: View?, viewGroup: ViewGroup): View {
        var v = minflater.inflate(R.layout.lv_iv_item, null)
        val iv = v.iv_item
        val tv = v.tv_time
        val params = iv.layoutParams as LinearLayout.LayoutParams
        params.height = width / 3
        iv.layoutParams = params
        if (i == mlistimg.size) {
            iv.setImageResource(R.mipmap.erp_photo)
        } else {
            var imgbean = mlistimg[i]
            Log.d("TAAG",imgbean.imguri)
            Myutil.settv(Myutil.getdata(imgbean.imgtime), tv)
            Myutil.showhttpphoto(imgbean.imguri, iv, mcontext)
        }
        return v
    }

    fun Allnotify(list: List<ImgBean>) {
        mlistimg = list
        notifyDataSetChanged()
    }
}
